Bode Plots

Here are several examples of generating Bode plots in Python.

As always, it's best to actually READ THE DOCUMENTATION.


In [8]:
%matplotlib notebook
import numpy as np
from scipy import signal 
import matplotlib.pyplot as plt

figsize = (8,4)

# Define a bunch of transfer functions

G1 = signal.TransferFunction([10], [1, 10])
G2 = signal.TransferFunction([1, 100], [1, 10])
G3 = signal.TransferFunction([1, 20], [1, 58, 407, 350]) # this one we'll draw in class
G4 = signal.TransferFunction([1, 2*0.2*10, 100],[1])
$$ G_1(s) = \frac{10}{s+10} $$$$ G_2(s) = \frac{s+100}{s+10} $$

For each transfer function we generate the frequency response.

This outputs the frequency, magnitude (decibels) and phase (degrees).

Then we simply plot it on a semilog axes.


In [9]:
w1, mag1, phase1 = signal.bode(G1, np.logspace(-2,4))

# Problem 2
fig, axarr=plt.subplots(1,2, figsize=figsize)
axarr[0].semilogx(w1,mag1)
axarr[0].set_title('Magnitude')
axarr[0].set_xlabel('Frequency (rad/sec)')
axarr[0].set_ylabel('Magnitude (db)')
axarr[0].grid(True)

axarr[1].semilogx(w1, phase1)
axarr[1].set_title('Phase')
axarr[1].set_xlabel('Frequency (rad/sec)')
axarr[1].set_ylabel('Phase (deg)')
axarr[1].grid(True)

plt.show()



In [10]:
w2, mag2, phase2 = signal.bode(G2, np.logspace(-2,4))

# Problem 2
fig, axarr=plt.subplots(1,2, figsize=figsize)
axarr[0].semilogx(w2,mag2)
axarr[0].set_title('Magnitude')
axarr[0].set_xlabel('Frequency (rad/sec)')
axarr[0].set_ylabel('Magnitude (db)')
axarr[0].grid(True)

axarr[1].semilogx(w2, phase2)
axarr[1].set_title('Phase')
axarr[1].set_xlabel('Frequency (rad/sec)')
axarr[1].set_ylabel('Phase (deg)')
axarr[1].grid(True)

plt.show()



In [11]:
w4, mag4, phase4 = signal.bode(G4, np.logspace(-2,4))

# Problem 2
fig, axarr=plt.subplots(1,2, figsize=figsize)
axarr[0].semilogx(w4,mag4)
axarr[0].set_title('Magnitude')
axarr[0].set_xlabel('Frequency (rad/sec)')
axarr[0].set_ylabel('Magnitude (db)')
axarr[0].grid(True)

axarr[1].semilogx(w4, phase4)
axarr[1].set_title('Phase')
axarr[1].set_xlabel('Frequency (rad/sec)')
axarr[1].set_ylabel('Phase (deg)')
axarr[1].grid(True)

plt.show()



In [12]:
w3, mag3, phase3 = signal.bode(G3, np.logspace(-2,4))

# Problem 2
fig, axarr=plt.subplots(1,2, figsize=figsize)
axarr[0].semilogx(w3,mag3)
axarr[0].set_title('Magnitude')
axarr[0].set_xlabel('Frequency (rad/sec)')
axarr[0].set_ylabel('Magnitude (db)')
axarr[0].grid(True)

axarr[1].semilogx(w3, phase3)
axarr[1].set_title('Phase')
axarr[1].set_xlabel('Frequency (rad/sec)')
axarr[1].set_ylabel('Phase (deg)')
axarr[1].grid(True)

plt.show()



In [13]:
mag3


Out[13]:
array([ -24.8612032 ,  -24.86153816,  -24.86212679,  -24.86316114,
        -24.86497844,  -24.86817059,  -24.87377535,  -24.8836089 ,
        -24.90083958,  -24.93096386,  -24.98342425,  -25.07416953,
        -25.22936055,  -25.48982065,  -25.9141433 ,  -26.5756159 ,
        -27.54733119,  -28.87760395,  -30.57258234,  -32.60383891,
        -34.9359431 ,  -37.55111998,  -40.45367457,  -43.65070679,
        -47.11962247,  -50.78761923,  -54.54687063,  -58.30240267,
        -62.02212029,  -65.75303354,  -69.58961665,  -73.61679407,
        -77.86873012,  -82.32664618,  -86.94361548,  -91.66995665,
        -96.46605279, -101.30460219, -106.16827102, -111.04655848,
       -115.93327199, -120.82481534, -125.71911842, -130.61499551,
       -135.51176942, -140.40905402, -145.3066293 , -150.20437004,
       -155.10220492, -160.00009337])